package com.plumejob.server.register.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plumejob.server.admin.vo.JobQ;
import com.plumejob.server.register.modal.PlumejobDetail;
import com.plumejob.server.register.pojo.PlumeJob;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Frank.chen
 */
@Repository
public interface PlumeJobDao extends BaseMapper<PlumeJob> {
    @Select("select app_name appName,job_name jobName,address,running,active,cron_expr cronExpr,job_method jobMethod," +
            "extra_data extraData,job_class jobClass from plume_job where app_name= #{appName}")
    List<PlumeJob> findByAppName(String  appName);

    @Insert("insert into plume_job (app_name,job_name,address,running,active,cron_expr,job_method,extra_data,job_class,last_fire_time,last_end_time,next_fire_time)" +
            " select #{appName}, #{jobName}, #{address}, #{running}, #{active}, #{cronExpr}, #{jobMethod}, #{extraData}, #{jobClass}, #{lastFireTime}, #{lastEndTime}, #{nextFireTime}" +
            " from dual where not exists(select id from plume_job where app_name=#{appName} and job_name=#{jobName})")
    int saveJob(PlumeJob job);

    @Update("update plume_job set address = #{address}, running = 1, last_fire_time=#{lastFireTime}, last_end_time=#{lastEndTime}, next_fire_time=#{nextFireTime} where app_name=#{appName} and job_name=#{jobName}")
    int updateScheme(PlumejobDetail jd);

    IPage<PlumeJob> pageList(IPage<PlumeJob> page,  @Param("q") JobQ q);
}
